package com.ppblock.shirox.conf;

import com.ppblock.shirox.core.AuthorizationRealm;
import com.ppblock.shirox.core.SubjectManager;
import com.ppblock.shirox.core.impl.AuthorizationManager;
import com.ppblock.shirox.interceptor.AuthorizationInterceptor;
import com.ppblock.shirox.surpport.ShiroxConfigure;
import com.ppblock.shirox.thymeleaf.dialect.ShiroxDialect;
import com.ppblock.shirox.utils.AuthorizationUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author yangjian
 * @since 17-12-11.
 */
@Configuration
public class MyShiroxConf extends WebMvcConfigurerAdapter {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new AuthorizationInterceptor()).addPathPatterns("/**").excludePathPatterns("/error");
		super.addInterceptors(registry);
	}

	@Bean
	public AuthorizationRealm shiroxRealm(){
		ShiroRealm shiroRealm = new ShiroRealm();
		return shiroRealm;
	}

	@Bean
	public SubjectManager subjectManger() {
		AuthorizationManager manager = new AuthorizationManager();
		manager.addRealm(shiroxRealm());
		ShiroxConfigure configure = new ShiroxConfigure();
		configure.setLoginUrl("/login");
		configure.setUnAuthorizedUrl("/403");
		manager.setShiroxConfigure(configure);
		AuthorizationUtils.bindAuthorizationManager(manager);
		return manager;
	}

	@Bean
	public ShiroxDialect shiroxDialect() {
		return new ShiroxDialect();
	}
}
